<?php
	namespace ampleharvest\task;

	require_once dirname(dirname(dirname(__FILE__))) . "/abstract_classes/abs_Task.php";
	require_once dirname(dirname(dirname(__FILE__))) . '/config/cConfig.php';
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cStomp.php';
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cAmpleHarvestLog.php';
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/notification/cMailNotification.php';

	class send_Email extends \abs_Task
	{
		const C_STR_NAME = "send_Email";
		public function __construct($str_LockPath)
		{
			$this->set_LockPath($str_LockPath);
			$this->set_Name(self::C_STR_NAME);
			parent::__construct();
		}

		public function run()
		{
			parent::run();

			try{
			$obj_Stomp = new \cStomp();
			}
			catch(\Exception $e)
			{
				$this->log_Error("Failed to connect to stomp server.");
				return false;
			}
			$obj_Email = new \cMailNotification();

			$obj_Stomp->subscribe(\cStomp::C_STR_QUEUE_EMAIL);

			while ($obj_Msg = $obj_Stomp->getMessage())
			{
				$this->log("Pulled Email from Queue", \cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

				$bool_Return = $obj_Email->send_JSONMail($obj_Msg->body);

				if ($bool_Return)
				{
					$this->log("Email Sent", \cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
				}
				else
				{
					$this->log_Error("Email Not Sent");
					print("Email Not Sent");
					return false;
				}

				$obj_Stomp->ackMessage($obj_Msg);
			}

			return true;
		}
	}
?>